package com.example.myapplication

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import androidx.viewbinding.ViewBinding
import com.example.myapplication.databinding.ActivityMainBinding

class MainActivity<T : ViewBinding> : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var navController: NavController

    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 获取navController，通过Activity空白的NavHostFragment获取
        navController = findNavController(R.id.nav_host_fragment)

        // FragmentContainerView 使用方式
        // val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
        // val navController = (navHostFragment  as NavHostFragment).navController

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home,
                R.id.navigation_dashboard,
                R.id.navigation_notifications,
                R.id.navigation_center
            )
        )
        // 将AppBarConfiguration和NavController绑定起来
        setupActionBarWithNavController(navController, appBarConfiguration)
        // 将BottomNavigationView和NavController绑定起来
        binding.navView.setupWithNavController(navController)

        navController.addOnDestinationChangedListener { _, destination, _ ->
            when (destination.id) {
                R.id.navigation_home,
                R.id.navigation_dashboard,
                R.id.navigation_notifications,
                R.id.navigation_center -> binding.navView.visibility = View.VISIBLE
                else -> binding.navView.visibility = View.GONE
            }
        }

        /**
         * 导航栏点击事件拦截，如判断是否登录
         */
        binding.navView.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.navigation_center -> {
                    if (!isCompleted) {
                        startActivity(Intent(this, SecondActivity::class.java))
                        return@setOnNavigationItemSelectedListener false
                    }
                }
            }

            return@setOnNavigationItemSelectedListener NavigationUI.onNavDestinationSelected(
                it,
                navController
            )
        }
    }

    /**
     * 这里重写onSupportNavigationUp()方法，目的时将back时间委托出去
     * 若堆栈中由两个以上Fragment，点击back键就会返回到上一个Fragment
     */
    override fun onSupportNavigateUp(): Boolean {
        val fragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
        return if (fragment != null) {
            NavHostFragment.findNavController(fragment).navigateUp()
        } else {
            super.onSupportNavigateUp()
        }
    }

    /**
     * 返回事件处理
     */
    override fun onBackPressed() {
        when (navController.currentDestination?.id ?: -1) {
            R.id.navigation_home,
            R.id.navigation_dashboard,
            R.id.navigation_notifications,
            R.id.navigation_center -> finish()
            else -> super.onBackPressed()
        }
    }

    override fun finish() {
        moveTaskToBack(false)
    }

    companion object {
        var isCompleted = false
    }

}